package com.cjd.common.fragment

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.cjd.common.activity.BaseActivity
import com.cjd.common.utils.DisplayUtil
import com.cjd.common.utils.NotchScreenUtil
import com.cjd.common.utils.PermissionUtil
import org.greenrobot.eventbus.EventBus

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/7/2
 * description fragment 基类
 */
abstract class BaseFragment : Fragment() {
    private var init = false
    protected var mRoot: View? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
//        if (root == null)
        mRoot = inflater.inflate(getLayoutResId(), container, false)
        mRoot?.let {
            if (it.parent != null && it.parent is ViewGroup) {
                (it.parent as ViewGroup).removeView(it)
            }
        }
        return mRoot
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        init = true
        initData(view)
    }


    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (init) {
            onSwitchFragment(isVisibleToUser)
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (hasEvent())
            EventBus.getDefault().register(this)
    }

    override fun onResume() {
        super.onResume()
        hasNotchPadding()?.let {
            setNotchPadding()
        }
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)

    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (hasEvent())
            EventBus.getDefault().unregister(this)
    }

    fun onRuntimePermissionRequest(
        activity: Activity,
        listener: ((unauthorized: List<String>) -> Unit),
        vararg permissions: String
    ) {
        PermissionUtil.requestPermissions(activity, listener, *permissions)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    protected abstract fun getLayoutResId(): Int

    protected abstract fun initData(view: View)

    protected open fun hasEvent() = false

    /**
     * view 初始化后 才会执行 ，即切换时才会调用
     * @param isVisible true fragment 可见
     */
    open fun onSwitchFragment(isVisible: Boolean) {

    }

    fun setStatusBarTextColor(isLight: Boolean, bgColor: Int = Color.TRANSPARENT) {
        activity?.let {
            it as BaseActivity
            it.setStatusBarTextColor(isLight, bgColor)
        }
    }

    /**
     * 需要添加 刘海屏 padding 的view  按需使用
     * tips: 正常情况下 只有沉浸模式 才需要计算刘海高度
     */
    open fun hasNotchPadding(): View? {
        return null
    }

    private fun setNotchPadding() {
        val view = hasNotchPadding()
        view?.let {
            var notchHeight = NotchScreenUtil.getNotchHeight(activity)
            if (activity is BaseActivity) {
                val baseActivity: BaseActivity = activity as BaseActivity
                if (notchHeight <= 0 && baseActivity.isImmersion)
                    notchHeight = DisplayUtil.getStatusBarHeight(requireContext())
            }
            if (notchHeight > 0) {
                view.setPadding(0, notchHeight, 0, 0)
            }
        }
    }
}